<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="库房名称" prop="wareHouseId">
          <el-select clearable v-model="queryParams.wareHouseId"  @change="chageHouse" placeholder="请选择库房名称">
            <el-option
              v-for="item in wareHouseoptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
      </el-form-item>

      <el-form-item label="存储柜名称" prop="cabinetId">
        <el-select v-model="queryParams.cabinetId" placeholder="请选择存储柜">
          <el-option
            v-for="item in cabinetoptions"
            :key="item.id"
            :label="item.cabinetName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="物品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入物品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="rfid" prop="rfid">
        <el-input
          v-model="queryParams.rfid"
          placeholder="请输入rfid"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="层数" prop="layer">
        <el-input
          v-model="queryParams.layer"
          placeholder="请输入层数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盒号" prop="boxNo">
        <el-input
          v-model="queryParams.boxNo"
          placeholder="请输入盒号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否出库" prop="outboundFlag">
        <el-select v-model="queryParams.outboundFlag" placeholder="请选择存储柜">
          <el-option
            v-for="item in outboundoptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['archive:product:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['archive:product:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['archive:product:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['archive:product:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" /> -->
      <!-- <el-table-column label="库房ID" align="center" prop="warehouseId" /> -->
      <el-table-column label="物品名称" align="center" prop="productName" />
      <el-table-column label="物品名称序号" align="center" prop="code" />
      <el-table-column label="库房名称" align="center" prop="warehouseName" />
      <el-table-column label="柜体编号" align="center" prop="cabinetCode" />
      <el-table-column label="盒号" align="center" prop="boxNo" />
      <el-table-column label="rfid" align="center" prop="rfid" />
      <el-table-column label="启用时间" align="center" prop="startTime" />
      <el-table-column label="停用时间" align="center" prop="endTime" />
      <el-table-column label="是否出库" align="center" prop="outboundFlag">
      <template slot-scope="scope">
        <!-- <p>圣道行</p> -->
        <span v-if="scope.row.outboundFlag=='1'">未出库</span>
        <span v-if="scope.row.outboundFlag=='0'">已出库</span>
      </template>
    </el-table-column>
         <el-table-column label="keyId" align="center" prop="keyId" />
         <el-table-column label="是否一致" align="center" prop="errorFlag" >
        <template slot-scope="scope">
        <span v-if="scope.row.errorFlag=='1'">不一致</span>
        <span v-else>一致</span>
        </template>
        </el-table-column>
        
    
      <el-table-column label="备注" align="center" prop="remark" />
      <!-- <el-table-column label="物品是否需要有效期(1需要,2,不需要)" align="center" prop="enableExpire" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['archive:product:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['archive:product:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleloaction(scope.row)"
            v-hasPermi="['archive:product:edit']"
          >选择位置</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="库房ID" prop="warehouseId">
          <el-input v-model="form.warehouseId" placeholder="请输入库房ID" />
        </el-form-item>
        <el-form-item label="库房名称" prop="warehouseName">
          <el-input v-model="form.warehouseName" placeholder="请输入库房名称" />
        </el-form-item>
        <el-form-item label="柜体ID" prop="cabinetId">
          <el-input v-model="form.cabinetId" placeholder="请输入柜体ID" />
        </el-form-item> -->
        <el-form-item label="物品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入物品名称" />
        </el-form-item>
        <el-form-item label="物品序号" prop="code">
          <el-input v-model="form.code" placeholder="请输入物品名称序号" />
        </el-form-item>
        <el-form-item label="rfid" prop="rfid">
          <el-input v-model="form.rfid" placeholder="请输入rfid" />
        </el-form-item>
        
        <!-- <el-form-item label="层数" prop="layer">
          <el-input v-model="form.layer" placeholder="请输入层数" />
        </el-form-item>
        <el-form-item label="盒号" prop="boxNo">
          <el-input v-model="form.boxNo" placeholder="请输入盒号" />
        </el-form-item> -->
        <!-- <el-form-item label="是否出库(0:未出库，1:已出库)" prop="outboundFlag">
          <el-input v-model="form.outboundFlag" placeholder="请输入是否出库(0:未出库，1:已出库)" />
        </el-form-item> -->
         <el-form-item label="启用时间" prop="startTime">
          <el-date-picker clearable size="small" style="width: 100%"
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择过期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="停用时间" prop="endTime">
          <el-date-picker clearable size="small" style="width: 100%"
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择过期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="物品属性" prop="productPropList">
          <div v-for="(item, index) in form.productPropList" :key="index" >
            <el-row style="margin-top: 10px;text-align: left">
              <el-col :span="8" style="margin-right:10px">
                <el-input v-model="item.propName" placeholder="请输入属性名称" />
              </el-col>
              <el-col :span="8" style="margin-right:10px">
                <el-input v-model="item.propValue" placeholder="请输入属性值" />
              </el-col>
              <el-col :span="3">
                <el-button @click="removeInput(index)">删除</el-button>
              </el-col>
              <el-col :span="3">
                <el-button @click="addInput">添加</el-button>
              </el-col>
            </el-row>
         
        </div>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <!-- <el-form-item label="物品是否需要有效期(1需要,2,不需要)" prop="enableExpire">
          <el-input v-model="form.enableExpire" placeholder="请输入物品是否需要有效期(1需要,2,不需要)" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
     <!-- 选择位置 -->
     <el-dialog title="请选位置" :visible.sync="openlocation" width="500px" append-to-body>
      <el-form ref="formlocation" :model="formlocation" :rules="ruleslocation" label-width="90px">
          <el-form-item label="库房" prop="wareHouseId">
          <el-select v-model="formlocation.wareHouseId" style="width:100%"  @change="chageselectHouse"  placeholder="请选择库房">
          <el-option
            v-for="item in wareHouseoptions"
            :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
        </el-form-item>
         <el-form-item label="柜体" prop="cabinetId">
           <el-select v-model="formlocation.cabinetId" placeholder="请选择柜体" @change="chageselectbox"  style="width:100%"  >
          <el-option
            v-for="item in selectcabinetoptions"
            :key="item.id"
            :label="item.cabinetName"
            :value="item.id">
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="格子编号" prop="cabinetId">
           <el-select v-model="formlocation.boxNo" placeholder="请选择柜体" style="width:100%"  >
          <el-option
            v-for="item in boxoptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormlocation">确 定</el-button>
        <el-button @click="cancellocation">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct,getInfoProduct } from "@/api/archive/product";
import {listwareHouse } from "@/api/archive/house";
import {cabinetlist } from "@/api/archive/cabinet";
export default {
  name: "Product",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品管理表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseId: null,
        warehouseName: null,
        cabinetId: null,
        productName: null,
        code: null,
        rfid: null,
        layer: null,
        boxNo: null,
        outboundFlag: null,
      },
      // 表单参数
      form: {
        productPropList:[{
        propName:'',
        propValue:''
      }]
      },
      // 表单校验
      rules: {
        productName: [
          { required: true, message: "请输入物品名称", trigger: "blur" }
        ],
        code: [
          { required: true, message: "请输入物品编号", trigger: "blur" }
        ],
        // rfid: [
        //   { required: true, message: "请输入rfid", trigger: "blur" }
        // ],
        // startTime: [
        //   { required: true, message: "选择启用时间", trigger: "blur" }
        // ],
        // endTime: [
        //   { required: true, message: "选择停用时间", trigger: "blur" }
        // ],


      },
      wareHouseoptions:[],
      cabinetoptions:[],
      selectcabinetoptions:[],
      queryparmcabinet :{
        wareHouseId:''
      },
      queryparmselectcabinet:{
        wareHouseId:''
      },
      outboundoptions:[{
        name :"未出库",
        id:'1'
      },{
        name :"已出库",
        id:'0'
      }
    ],
    formlocation:{
      wareHouseId:undefined,
      cabinetId:undefined
    },
    openlocation:false,
    boxoptions:[],
    ruleslocation:{
          wareHouseId: [
          { required: true, message: "请选择库房", trigger: "blur" }
        ],
          cabinetId: [
          { required: true, message: "请选择柜体", trigger: "blur" }
        ]
      },
      selectproduct:null//选中的商品
    };
    
  },
  created() {
    this.getList();
    this.wareHouse()
  },
  methods: {
    wareHouse() {
      listwareHouse().then(response => {
        this.wareHouseoptions = response.data;
      });
    },
         //选择库房
         chageHouse(e){
      this.queryParams.wareHouseId=e
       cabinetlist(this.queryParams).then(response => {
        this.cabinetoptions = response.data;
      });
    },
    //选择格子
    chageselectHouse(e){
      console.log(e)
      this.queryparmselectcabinet.wareHouseId=e
      this.selectcabinetoptions=[]
      this.formlocation.cabinetId=''
       cabinetlist(this.queryparmselectcabinet).then(response => { 
       this.selectcabinetoptions=response.data
      });
    },
     chageselectbox(e){
      console.log(e)
      this.boxoptions=[]
      for(var i in this.selectcabinetoptions){
        if(this.selectcabinetoptions[i].id==e){
             for(let a=0;a<this.selectcabinetoptions[i].boxNo;a++){
            this.boxoptions.push({
              name: "格子编号:"+(a+1),
              id:(a+1)
            })
          }
        }
      }

   
    },
    /** 查询商品管理列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //添加删除属性
    addInput() {
      this.form.productPropList.push({ propName: '',propValue:'' });
    },
    removeInput(index) {
      if(this.form.productPropList.length>1){
        this.form.productPropList.splice(index, 1);
      }

    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        warehouseId: null,
        warehouseName: null,
        cabinetId: null,
        productName: null,
        code: null,
        rfid: null,
        layer: null,
        boxNo: null,
        outboundFlag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        enableExpire: null,
        productPropList:[{
        propName:'',
        propValue:''
      }]
    
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProduct(id).then(response => {
        if(response.data.productPropList==null){
          response.data.productPropList=[{
           propName:'',
           propValue:''
          }]
        }
        this.form = response.data;
        this.open = true;
        this.title = "修改商品管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品管理编号为"' + ids + '"的数据项？').then(function() {
        return delProduct(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
      //选择位置
      handleloaction(row){
        this.selectproduct=row
       this.formlocation = {
        wareHouseId:undefined,
        cabinetId:undefined,
        producttId:null
      };
      this.formlocation.producttId=row.id
      this.openlocation=true
    },
    //判断存储位置
    submitFormlocation(){
     
      getInfoProduct(this.formlocation.cabinetId,this.formlocation.boxNo).then(response => {
             if(response.data==undefined){
              this.selectproduct.cabinetId=this.formlocation.cabinetId
              this.selectproduct.outboundFlag='1'
              this.selectproduct.boxNo=this.formlocation.boxNo

              updateProduct(this.selectproduct).then(a => {
                this.$modal.msgSuccess("新增成功");
                this.openlocation=false
                this.getList();
               });
             }
             else{
              
           this.$modal.confirm('存储柜已存在商品是否确认替换存储柜商品？').then(function() {
         }).then(() => {
          response.data.cabinetId=''
          response.data.boxNo=''
         updateProduct(response.data).then(a => {
          this.selectproduct.cabinetId=this.formlocation.cabinetId
          this.selectproduct.boxNo=this.formlocation.boxNo

        
            this.selectproduct.outboundFlag='1'
          
              updateProduct(this.selectproduct).then(b => {
                this.$modal.msgSuccess("新增成功");
                this.openlocation=false
                this.getList();
               });
               });
             
            }).catch(() => {});
             }

            });
    },
    cancellocation() {
      this.openlocation = false;
     
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('archive/product/export', {
        ...this.queryParams
      }, `product_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
